const tools = require("../lib/tools") 
const $data = require("../data") 
const $weixin = require("../lib/weixin.js") ;
module.exports = function (router) {
// 从查询
// router.get('/snogogo_nuxt/api/get_topic_by_keywords', async function (ctx, next) {
//     let res = $data.getDocsBykeywords(""); 
//     ctx.body = tools.response(res.status,res.statusText,res.data);
//   })
  // post
  router.post('/snogogo_nuxt/api/post', async function (ctx, next) {
   
    let params = ctx.request.body;
    
    if(params.SnoGoGoApikey=="09bcedd8-7ae1-42e0-a8c5-199f038b8952"){
       params._uid = params.uid;
    }else {
        let sesData = await $data.sesData(ctx.sid)
        if(!sesData.uid){
          ctx.body = tools.response(2,"Login Frist");
          return;
        }
        params._uid = sesData.uid;
    }
   
    // msg_sec_check
    let msg_sec_check_params = {
      'openid':params.openid,
      'content': params.title ? (params.title + params.content) : params.content,
    }
    let msg_sec_check_res =  await $weixin.msg_sec_check(msg_sec_check_params);
    if(msg_sec_check_res.code==0 && msg_sec_check_res.data.suggest_type=="risky"){
      ctx.body = tools.response(4,"发布失败:内容包含敏感字,请修改后再发布",msg_sec_check_res);
      return;
    }
    if(msg_sec_check_res.code>0){
      console.log("msg_sec_check_res:",msg_sec_check_params,msg_sec_check_res);
    }
   
    // console.log("params:",params);
    let res = {};
    let extend = params.extend?params.extend:{};
    if(params.cover!=""){
      extend.cover = params.cover;
    }
    if( typeof params.userCategoryId =="string"){
      extend.userCategoryId = params.userCategoryId;
    }
    
    if(params.materials){
      // let materials = [];
      // for (const key in params.materials) {
      //   materials.push({
      //     url:params.materials[key].key?params.materials[key].key:params.materials[key].url,
      //     type:params.materials[key].type,
      //     caption:params.materials[key].caption,
      //   });
      // }
      extend.materials =JSON.stringify( params.materials);
    }
   
    let post = {
      tags:params.tags,
       title:params.title,
       _uid:params._uid,
       cid:params.cid,
       content:params.content,
       extend:extend
    }
    
    
    if(params.mainPid){
      post.mainPid = params.mainPid;
    }
    if(params.pid){
      post.pid = params.pid;
    }
    if(params.tid){
      post.tid = params.tid;
    }
    if(params.toPid){
      post.toPid = params.toPid;
    }
    if(params.type=="topic"){
      post.extend.description = params.content;
    }
    
    if( params['skiResortId']){
      // 评分
      res =  await $data.rateSkiResort(params['skiResortId'],extend);
      if(res.code==0){
        post.extend.skiResortId = params.skiResortId;
        res = await $data.reply(post); 
      }
     
    }else if(params['type']=="topic"){
      res = await $data.newTopic(post); 
    } else{
      res = await $data.reply(post); 
    }
    // ////console.log("res:",res)
    ctx.body = tools.response(res.code,res.msg,res.data);
  })
  
  // vote on a post
  // 从查询
  router.post('/snogogo_nuxt/api/vote', async function (ctx, next) {
    let params = ctx.request.body;
    let sesData = await $data.sesData(ctx.sid)
    if(!sesData.uid){
      ctx.body = tools.response(2,"Login Frist");
      return;
    }
    let pid = params['pid'];
    let res =await $data.vote(pid,params['type'],sesData.uid);
    ctx.body = tools.response(res.code,res.msg,res.data);
  })

   // 从查询
   router.post('/snogogo_nuxt/api/bookmark', async function (ctx, next) {
    let params = ctx.request.body;
    let sesData = await $data.sesData(ctx.sid)
    if(!sesData.uid){
      ctx.body = tools.response(2,"Login Frist");
      return;
    }
    let pid = params['pid'];
    let res =await $data.bookmark(pid,params['type'],sesData.uid);
    ctx.body = tools.response(res.code,res.msg,res.data);
  })


  router.post('/snogogo_nuxt/api/flag', async function (ctx, next) {
    let params = ctx.request.body;
    let sesData = await $data.sesData(ctx.sid)
    if(!sesData.uid){
      ctx.body = tools.response(2,"Login Frist");
      return;
    }
   
    let res =await $data.flag( params , sesData.uid);
    ctx.body = tools.response(res.code,res.msg,res.data);
  })
  
//   写一个
router.post('/snogogo_nuxt/api/getDocsWithScore', async function (ctx, next) {
  let params = ctx.request.body;
  let page =  typeof(params['page'])!="number" ? 1: params['page'];
  let size = 20;
  let p ={
    page:page,
    type:'topic',
    size:20,
    from:(page-1)*size
  }
  if(params['type']=="user"){
     p.type = "user";
  }else if(params['type']=="topic"){
      p.type = "topic";
  }else if(params['type']=="all"){
    delete( p.type)  
  } 
  
 if( params['keywords']){
    p.keywords = params['keywords'];
  }
  if( params['cid']){
    p.cid = params['cid'];
  }
  if( params['cover']){
    p.cover = params['cover'];
  }
  if( params['address']){
    p.address = params['address'];
  }
  if( params['tags']){
    p.tags = params['tags'];
  }
  if( params['sort']){
    p.sort = params['sort'];
  }
  ////console.log("$data.getDocsWithScore p :",p)
  let res ={};
  if(p.keywords  instanceof Array ){
      res =await $data.getDocsWithScore(p);
  }else{
      res =await $data.getDocsBykeywords(p);
  }
  
  ctx.body = tools.response(res.code,res.msg,res.data);
})


// 

//   search_resorts
router.post('/snogogo_nuxt/api/search_resorts', async function (ctx, next) {
  let params = ctx.request.body;
  let page =  typeof(params['page'])!="number" ? 1: params['page'];
  let size = 50;
  let p ={
    type:"topic",
    page:page,
    size:size,
    cid:3,
    sort:"votes",
    from:(page-1)*size
  }
  if( params['keywords']){
    p.keywords = params['keywords'];
  }
  let res = await $data.getDocsBykeywords(p);
  if(res.code==0 && res.data.list){
     res.data = res.data.list.map(x=>{
        return x.title;
     })
  }else{
    res.data = [];
  }
  // if(!p.keywords && res.data.length>0){
  //     $redis.set("cache:",res.data)
  // }
  
  ctx.body = tools.response(res.code,res.msg,res.data);
})




  // vote on a post
  // 从查询
  router.post('/snogogo_nuxt/api/vote', async function (ctx, next) {
    let params = ctx.request.body;
    let sesData = await $data.sesData(ctx.sid)
    if(!sesData.uid){
      ctx.body = tools.response(2,"Login Frist");
      return;
    }
    let pid = params['pid'];
    let res =await $data.vote(pid,params['type'],sesData.uid);
    ctx.body = tools.response(res.code,res.msg,res.data);
  })

  // 删除Topic
  router.post('/snogogo_nuxt/api/topic/delete', async function (ctx, next) {
    let params = ctx.request.body;
    let sesData = await $data.sesData(ctx.sid)
    if(!sesData.uid){
      ctx.body = tools.response(2,"Login Frist");
      return;
    }
   
    let res =await $data.delTopic( params.tid , sesData.uid);
    ctx.body = tools.response(res.code,res.msg,res.data);
  })
// 删除一个Post
  router.post('/snogogo_nuxt/api/post/delete', async function (ctx, next) {
    let params = ctx.request.body;
    let sesData = await $data.sesData(ctx.sid)
    if(!sesData.uid){
      ctx.body = tools.response(2,"Login Frist");
      return;
    }
   
    let res =await $data.delPost( params.pid , sesData.uid);
    ctx.body = tools.response(res.code,res.msg,res.data);
  })

  // 用户主页上的文章
  router.post('/snogogo_nuxt/api/topic/userIndex', async function (ctx, next) {
    let params = ctx.request.body;
    let page = params.page ? params.page :1;
    let list =await $data.userIndexTopic( params.uid ,page);
    ctx.body = tools.response("0","suc",list);
  })


  router.post('/snogogo_nuxt/api/topic/userCid', async function (ctx, next) {
    let params = ctx.request.body;
    let page = params.page ? params.page :1;
    let q={
      userCategoryId:params.userCategoryId,
      type:'topic',
      size:12,
      from:(page-1)*12
    }
    var res = await $data.getDocsBykeywords(q);
    ctx.body = tools.response(res.code,res.msg,res.data);
  })

//删除分类
  router.post('/snogogo_nuxt/api/topic/user/delCid', async function (ctx, next) {
      let sesData = await $data.sesData(ctx.sid)
      if(!sesData.uid){
        ctx.body = tools.response(2,"Login Frist");
        return;
      }
      let params = ctx.request.body;     
      var res = await $data.delUserCategory(sesData.uid,params.category_name);
      ctx.body = tools.response(res.code,res.msg,res.data);
  })

  //修改分类
  router.post('/snogogo_nuxt/api/topic/user/editCid', async function (ctx, next) {
    let sesData = await $data.sesData(ctx.sid)
    if(!sesData.uid){
      ctx.body = tools.response(2,"Login Frist");
      return;
    }
    let params = ctx.request.body;     
    var res = await $data.updateUserCategory(sesData.uid,params.old_category_name,params.category_name);
    ctx.body = tools.response(res.code,res.msg,res.data);
})


router.get('/apiv2/topic/id', async function (ctx, next) {
  let res = {};
  if(ctx.query.pid>0){
     res = await $data.getPost(ctx.query.pid);
     ctx.query.tid = res.data.tid
  }

  if(ctx.query.tid>0){
    res = await $data.getTopic(ctx.query.tid);
  }
  
  
  ctx.body = tools.response(res.code,res.msg,res.data);
})




}